package com.turing.system.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.turing.system.entity.Customer;
import com.turing.system.entity.CustomerWithdrawals;
import com.turing.system.feign.AuthService;
import com.turing.system.feign.CustomerService;
import com.turing.system.mapper.CustomerMapper;
import com.turing.system.service.CustomerWithdrawalsService;
import com.turing.system.mapper.CustomerWithdrawalsMapper;
import enums.ErrorCode;
import org.springframework.stereotype.Service;
import result.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author dell
* @description 针对表【customer_withdrawals(客户流水)】的数据库操作Service实现
* @createDate 2023-05-08 15:50:59
*/
@Service
public class CustomerWithdrawalsServiceImpl extends ServiceImpl<CustomerWithdrawalsMapper, CustomerWithdrawals>
    implements CustomerWithdrawalsService{

    @Resource
    private CustomerWithdrawalsMapper customerWithdrawalsMapper;
    @Resource
    private AuthService authService;
    @Resource
    private CustomerService customerService;
    @Resource
    private HttpServletRequest request;


    @Override
    public Result cgCoinChargeList(String cusNickname, Integer pageNum, Integer pageSize) {
        List<Map> cwList ;
        //传了用户昵称
        if(cusNickname != null && cusNickname != ""){
            //用户名不正确
            List<String> customerIds = this.baseMapper.selectIds(cusNickname);
            System.out.println("customerIds = " + customerIds);
            if(customerIds == null){
                return Result.fail(ErrorCode.ERROR_USERNAME_UNKOWN);
            }
            //该用户没有数据
            ArrayList<Object> list = new ArrayList<>();
            for (int i = 0; i < customerIds.size(); i++) {
                list.add(customerIds.get(i));
            }
            LambdaQueryWrapper<CustomerWithdrawals> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.in(CustomerWithdrawals::getTargetId,list)
                    .eq(CustomerWithdrawals::getCwInfo,"官方充值");
            List<CustomerWithdrawals> customerWithdrawals = this.baseMapper.selectList(wrapper1);
            System.out.println("customerWithdrawals = " + customerWithdrawals);
            if(customerWithdrawals.isEmpty()){
                return Result.fail(ErrorCode.NODATA);
            }
            //该用户有数据
            //有分页
            if(pageNum != null && pageSize != null){
                PageHelper.startPage(pageNum,pageSize);
                cwList = this.baseMapper.cgCoinChargeList(cusNickname);
                PageInfo pageInfo = new PageInfo(cwList);
                return Result.success(pageInfo);
            }else{
                //无分页
                cwList = this.baseMapper.cgCoinChargeList(cusNickname);
                return Result.success(cwList);
            }
        }else {
            //有分页
            if(pageNum != null && pageSize != null){
                PageHelper.startPage(pageNum,pageSize);
                cwList = this.baseMapper.selectAll();
                PageInfo pageInfo = new PageInfo(cwList);
                return Result.success(pageInfo);
            }else{
                //无分页
                cwList = this.baseMapper.selectAll();
                return Result.success(cwList);
            }
        }

    }

    @Override
    public Result cwList(Integer state, Integer type, Integer pageNum, Integer pageSize,Integer infoType) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        //获取token
        String token = request.getHeader("Authorization");
        String tokenHead = "Bearer ";
        token = token.substring(tokenHead.length());
        System.out.println("token = " + token);
        //根据token获取id
        Result infoByToken = authService.getUserInfoByToken(token);
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(infoByToken.getData()));
        String username = jsonObject.getString("username");
        List<Map> list = super.baseMapper.cwList(state,type,infoType,username);
        List<Map> list1 = new ArrayList<>();
        for (Map map : list) {
            String cw_info = (String) map.get("cw_info");
            if (infoType == 1){
                if ("商家提现收入%".equals(cw_info) || "客官付支出%".equals(cw_info) || "购买商品消费%".equals(cw_info)){
                    String s = cw_info.substring(0, cw_info.lastIndexOf("%"));
                    map.put("cwInfo",s);
                }
            }
            list1.add(map);
        }

        if (list1.size() > 0){
            return Result.success(list1);
        }else {
            return Result.fail(0,"无数据");
        }
    }

    @Override
    public Result cwAddSave(JSONObject jsonObject) {
        CustomerWithdrawals customerWithdrawals = JSONObject.toJavaObject(jsonObject, CustomerWithdrawals.class);
        int insert = customerWithdrawalsMapper.insert(customerWithdrawals);
        if (insert > 0){
            return Result.success(insert);
        }else {
            return Result.fail(ErrorCode.ERROR_ADD);
        }
    }

    @Override
    public Result cwListForPc(String sourceId, String targetId, Integer pageNum, Integer pageSize, String cwInfo) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<Map> list = super.baseMapper.cwListForPc(sourceId,targetId,cwInfo);
        if (list.size() > 0){
            if (pageNum != null && pageSize != null){
                return Result.success(new PageInfo<>(list));
            }else {
                return Result.success(list);
            }
        }
        return Result.fail(0,"无数据");
    }
}




